package com.infra.micro.oss.domain.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.infra.common.resp.PageEx;
import com.infra.common.resp.Response;
import com.infra.common.util.BizUtils;
import com.infra.micro.oss.domain.db.entity.FileInfoPO;
import com.infra.micro.oss.domain.repository.FileInfoRepository;
import com.infra.micro.oss.domain.service.FileInfoService;
import com.infra.micro.oss.model.req.QueryFileInfoReq;
import com.infra.micro.oss.model.vo.FileInfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FileInfoServiceImpl extends BaseFileService implements FileInfoService {
    @Resource
    private FileInfoRepository fileInfoRepository;

    @Override
    public Response<FileInfoVO> queryByFileId(Long fileId) {
        FileInfoPO record = fileInfoRepository.queryByFileId(fileId);
        if (record == null) {
            return Response.error("文件ID无效，文件未找到");
        }
        return Response.success(buildFileInfoVO(record), "查询成功!");
    }

    @Override
    public Response<List<FileInfoVO>> queryByFileIds(List<Long> fileIds) {
        List<FileInfoPO> list = fileInfoRepository.queryByFileIds(fileIds);
        return Response.success(buildFileInfoVOs(list), "查询成功!");
    }

    @Override
    public Response<PageEx<FileInfoVO, QueryFileInfoReq>> queryByPage(QueryFileInfoReq req) {
        PageInfo<FileInfoPO> pageInfo = PageHelper.startPage(req.getPageIndex(), req.getPageSize(), BizUtils.buildOrderByClause(req.getSortField(), req.getSortType()))
                .doSelectPageInfo(() -> fileInfoRepository.queryByCond(req));

        return Response.success(PageEx.builder(pageInfo, req), "查询成功!");
    }
}
